package cn.bnuz.edu.todolist.database;

import android.os.StrictMode;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;

import cn.bnuz.edu.todolist.R;
import cn.bnuz.edu.todolist.pojo.Event;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class TodoDB {
    private static String target = "http://159.75.18.144:8080/TodoList_Server";

    public static List getEvent(){
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        StringBuilder res = new StringBuilder();
        URL url;
        try {
            url = new URL(target+"/show");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setConnectTimeout(100);
            InputStreamReader reader = new InputStreamReader(httpURLConnection.getInputStream());
            BufferedReader br = new BufferedReader(reader);
            String inputLine = null ;
            while ((inputLine = br.readLine()) != null) {
                res.append(inputLine);
            }
            String result = res.toString();
            Gson gson = new Gson();
            List<Event> list = gson.fromJson(result, new TypeToken<List<Event>>(){}.getType());
            reader.close();
            httpURLConnection.disconnect();
            return list;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List getTodayEvent(){
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        StringBuilder res = new StringBuilder();
        URL url;
        try {
            url = new URL(target+"/showToday");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setConnectTimeout(100);
            InputStreamReader reader = new InputStreamReader(httpURLConnection.getInputStream());
            BufferedReader br = new BufferedReader(reader);
            String inputLine = null ;
            while ((inputLine = br.readLine()) != null) {
                res.append(inputLine);
            }
            String result = res.toString();
            Gson gson = new Gson();
            List<Event> list = gson.fromJson(result, new TypeToken<List<Event>>(){}.getType());
            reader.close();
            httpURLConnection.disconnect();
            return list;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void changeEvent(int id) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

    }

    public static boolean addEvent(Event event){
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        String url = target + "/add";
        String title,time;
        HttpUrl.Builder httpBuilder = HttpUrl.parse(url).newBuilder();
        httpBuilder.addQueryParameter("title",event.getEventTitle());
        httpBuilder.addQueryParameter("time",event.getEventTime());
        Request request = new Request.Builder()
                .url(httpBuilder.build())
                .get()
                .build();
        try{
            OkHttpClient client = new OkHttpClient.Builder().connectTimeout(100, TimeUnit.MILLISECONDS).build();
            Response response = client.newCall(request).execute();
            System.out.println(response.body().toString());

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
